/*
 * =============================================================================
 * Copyright© by Benjamin Sempere,
 * All rights reserved.
 * =============================================================================
 * Author  : Benjamin Sempere
 * E-mail  : benjamin@sempere.org
 * Homepage: www.sempere.org
 * =============================================================================
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.sempere.gwt.toolbox.core.client.controller;

import org.sempere.gwt.toolbox.core.client.common.Startable;

import com.google.gwt.core.client.GWT;

public abstract class ApplicationController extends AbstractController implements Startable {

	public static final String APPLICATION_VIEW = "ApplicationView";

	/**
	 * Default class constructor
	 */
	public ApplicationController() {
	}

	public void preStart() {
		GWT.log("IN preStart.", null);
		this.initializeApplication();
	}

	public void start() {
		GWT.log("IN start.", null);
		this.startApplication();
	}

	public void stop() {
		GWT.log("IN stop.", null);
	}

	public void postStop() {
		GWT.log("IN postStop.", null);
	}

	/**
	 * Initialize the application
	 */
	protected final void initializeApplication() {
		// 1. Initialize needed services
		this.initializeServices();
		GWT.log("Services successfully initialized.", null);

		// 2. Initialize all needed models
		this.initializeModels();
		GWT.log("Models successfully initialized.", null);

		// 3. Initialize all needed views
		this.initializeViews();
		GWT.log("Views successfully initialized.", null);

		// 4. Initialize all needed controllers
		this.initializeControllers();
		GWT.log("Controllers successfully initialized.", null);

		// 5. Perform binding between views and models
		this.associateViewsAndModels();
		GWT.log("Views and models successfully bound.", null);
	}

	/**
	 * Initialize all needed services
	 */
	public abstract void initializeServices();

	/**
	 * Initialize all needed models
	 */
	public abstract void initializeModels();

	/**
	 * Initialize all needed views
	 */
	public abstract void initializeViews();

	/**
	 * Initialize all needed controllers
	 */
	public abstract void initializeControllers();

	/**
	 * Perform binding between views and models
	 */
	public abstract void associateViewsAndModels();

	/**
	 * Start the application
	 */
	public abstract void startApplication();

	public String getControllerName() {
		return "APPLICATION_CONTROLLER";
	}
}